package com.gis3c.sys.service.impl;

import com.gis3c.sys.dao.DictionaryDao;
import com.gis3c.sys.entry.po.Dictionary;
import com.gis3c.sys.entry.vo.DictionaryModel;
import com.gis3c.sys.service.DictionaryService;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by hukekuan on 2018/2/6.
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class DictionaryServiceImpl implements DictionaryService {
    @Autowired private Mapper baseMapper;
    @Autowired private DictionaryDao dictionaryDao;

    @Override
    public List<DictionaryModel> findDictionarysByType(String dictType) {
        List<DictionaryModel> dictList = new ArrayList<>();
        List<Dictionary> queryResult = dictionaryDao.findDictionarysByType(dictType);
        queryResult.forEach(dict -> dictList.add(baseMapper.map(dict,DictionaryModel.class)));
        return dictList;
    }

    @Override
    public List<DictionaryModel> findDictionarysByPage(String dictType, int pageSize, int currentPage) {
        List<DictionaryModel> dictList = new ArrayList<>();
        List<Dictionary> queryResult = dictionaryDao.findDictionarysByPage(dictType,pageSize,currentPage);
        queryResult.forEach(dict -> dictList.add(baseMapper.map(dict,DictionaryModel.class)));
        return dictList;
    }

    @Override
    public DictionaryModel findDictionaryById(String dictId) {
        Dictionary dictionary = dictionaryDao.findDictionaryById(dictId);
        return dictionary != null ? baseMapper.map(dictionary,DictionaryModel.class):null;
    }

    @Override
    public List<DictionaryModel> findDictionarysByParent(String parentId) {
        List<DictionaryModel> dictList = new ArrayList<>();
        List<Dictionary> queryResult = dictionaryDao.findDictionarysByParent(parentId);
        queryResult.forEach(dict -> dictList.add(baseMapper.map(dict,DictionaryModel.class)));
        return dictList;
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    public Integer insertDictionary(DictionaryModel dictionaryModel) {
        Dictionary newDict = baseMapper.map(dictionaryModel,Dictionary.class);
        newDict.setDictId(UUID.randomUUID().toString());
        return dictionaryDao.insertDictionary(newDict);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    public Integer deleteDictionaryById(String dictId) {
        return dictionaryDao.deleteDictionaryById(dictId);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    public Integer deleteDictionaryByType(String dictType) {
        return dictionaryDao.deleteDictionaryByType(dictType);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    public Integer deleteDictionaryByParent(String parentIdId) {
        return dictionaryDao.deleteDictionaryByParent(parentIdId);
    }
}
